package com.xzqwjw.taskmanager.utility;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Environment;
import android.os.StatFs;
import android.text.format.Time;

public class CommonHelper {

  public CommonHelper() {
  }

  private String getFileExtName(String strFileFullName) {
    if (strFileFullName.length() > 0 && strFileFullName != null) {
      int i = strFileFullName.lastIndexOf(".");
      if (i > -1 && i < strFileFullName.length()) {
        return strFileFullName.substring(i + 1);
      }
    }
    return "";
  }

  public static void writeImageToDisk(byte[] bytePhoto, String strFileName) {
    String strFilePath = "";
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
      strFilePath = Environment.getExternalStorageDirectory() + File.separator + "PartyInfo" + File.separator
          + "HeadPhoto" + File.separator + strFileName;
      try {
        File objFile = new File(strFilePath);
        FileOutputStream objFOS = new FileOutputStream(objFile);
        objFOS.write(bytePhoto);
        objFOS.flush();
        objFOS.close();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }

  public static byte[] readStream(InputStream objIS) throws Exception {
    byte[] aryBuffer = new byte[1024];
    int intLength = -1;
    ByteArrayOutputStream baosOS = new ByteArrayOutputStream();
    while ((intLength = objIS.read(aryBuffer)) != -1) {
      baosOS.write(aryBuffer, 0, intLength);
    }
    byte[] aryData = baosOS.toByteArray();
    baosOS.close();
    objIS.close();
    return aryData;
  }

  public static String getAgeFromBirthday(String strBirthday) {
    if (strBirthday != "") {
      int intYearNow, intYearBirthday;
      // ���ո�ʽһ���� 197003 ������ 1984.08��������� 19788
      Time timeNow = new Time();
      timeNow.setToNow();
      intYearNow = timeNow.year;

      intYearBirthday = Integer.parseInt(strBirthday.substring(0, 4));

      return "" + (intYearNow - intYearBirthday) + "��";
    } else {
      return "";
    }
  }

  public static String getBirthdayFromAge(String strAge) {
    if (strAge != "") {
      int intYearNow, intAge, intYearBirthday;

      // ���ո�ʽһ���� 197003 ������ 1984.08��������� 19788
      Time timeNow = new Time();
      timeNow.setToNow();
      intYearNow = timeNow.year;

      intAge = Integer.parseInt(strAge);

      intYearBirthday = intYearNow - intAge;

      return "" + intYearBirthday;
    } else {
      return "";
    }
  }

  // ͼ�������
  public static Bitmap setBytesToBMP(byte[] aryBytes, BitmapFactory.Options objBMPOptions) {
    if (aryBytes != null) {
      if (objBMPOptions != null)
        return BitmapFactory.decodeByteArray(aryBytes, 0, aryBytes.length, objBMPOptions);
      else
        return BitmapFactory.decodeByteArray(aryBytes, 0, aryBytes.length);
    } else {
      return null;
    }
  }

  public static byte[] setBMPToBytes(Bitmap objBMP) {
    if (objBMP != null) {
      ByteArrayOutputStream objBAOS = new ByteArrayOutputStream();
      objBMP.compress(Bitmap.CompressFormat.PNG, 100, objBAOS);
      return objBAOS.toByteArray();
    } else {
      return null;
    }
  }

  public static Bitmap zoomBMP(Bitmap objBMP, int intWidth, int intHeight) {
    if (objBMP != null) {
      int intBMPWidth = objBMP.getWidth();
      int intBMPHeight = objBMP.getHeight();
      Matrix objMatrix = new Matrix();
      float fltScaleWidth = ((float) intWidth / intBMPWidth);
      float fltScaleHeight = ((float) intHeight / intBMPHeight);
      objMatrix.postScale(fltScaleWidth, fltScaleHeight);
      Bitmap objNewBmp = Bitmap.createBitmap(objBMP, 0, 0, intBMPWidth, intBMPHeight, objMatrix, true);
      return objNewBmp;
    } else {
      return null;
    }
  }

  public static File getFileFromBytes(byte[] aryBytes, String strOutputFilePath) {
    BufferedOutputStream objBOS = null;
    File objFile = null;
    try {
      objFile = new File(strOutputFilePath);
      FileOutputStream objFOS = new FileOutputStream(objFile);
      objBOS = new BufferedOutputStream(objFOS);
      objBOS.write(aryBytes);
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (objBOS != null) {
        try {
          objBOS.close();
        } catch (IOException e1) {
          e1.printStackTrace();
        }
      }
    }
    return objFile;
  }

  public static Bitmap getBMPFromRES(Context objContext, int intResID) {
    BitmapFactory.Options objOption = new BitmapFactory.Options();
    objOption.inPreferredConfig = Bitmap.Config.RGB_565;
    objOption.inPurgeable = true;
    objOption.inInputShareable = true;
    // ��ȡ��ԴͼƬ
    InputStream objIS = objContext.getResources().openRawResource(intResID);
    return BitmapFactory.decodeStream(objIS, null, objOption);
  }

  public static void recycleBMP(Bitmap objBmp) {
    if (objBmp != null && !objBmp.isRecycled()) {
      objBmp.recycle();
      System.gc();
    }
  }

  public static Bitmap getBMPFromSDCard(String strFileName) {
    // ���SDCardĿ¼
    File objPath = Environment.getExternalStorageDirectory();
    return BitmapFactory.decodeFile(objPath + strFileName, null);
  }

  public static Bitmap getBMPFromASSETS(Context objContext, String strFileName) {
    InputStream objIS = null;
    try {
      objIS = objContext.getResources().getAssets().open(strFileName);
    } catch (IOException e) {
      e.printStackTrace();
    }
    return BitmapFactory.decodeStream(objIS);
  }

  public static Bitmap getBMPFromUrl(String strUrl) {
    URL objURL;
    InputStream objIS = null;
    try {
      objURL = new URL(strUrl);
      objIS = (InputStream) objURL.getContent();
    } catch (Exception e) {
      e.printStackTrace();
    }
    return BitmapFactory.decodeStream(objIS);
  }

  public static String setCNDate(String strDate) {
    String strReturn = "";
    SimpleDateFormat objSDF = new SimpleDateFormat("yyyy/MM/dd");
    try {
      Date dateTemp = objSDF.parse(strDate);
      // ��ȡ����ʵ��
      Calendar objCalendar = Calendar.getInstance();
      // ����������Ϊָ����ʱ��
      objCalendar.setTime(dateTemp);
      // ��ȡ��
      int intYear = objCalendar.get(Calendar.YEAR);
      // ��ȡ�£�����Ҫע�⣬�·��Ǵ�0��ʼ��
      int intMonth = objCalendar.get(Calendar.MONTH) + 1;
      // ��ȡ��
      int intDay = objCalendar.get(Calendar.DAY_OF_MONTH);
      // strReturn = intYear + "��" + intMonth + "��" + intDay + "��";
      strReturn = intYear + "��" + intMonth + "��";
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return strReturn;
  }

}